import axios from 'axios'
import { ElNotification } from 'element-plus'
import router from './router'

axios.defaults.baseURL = import.meta.env.VITE_API_URL
axios.defaults.timeout = 5000

export default {
    fetchJson: function (options) {
        let url = options.url
        let method = 'post'
        let data = options.data
        if (url == undefined || url == null || url == '') {
            throw '缺少url参数'
            return
        }

        if (data == undefined || data == null || data == '') {
            throw '缺少data参数'
            return
        }
        return new Promise((resolve, reject) => {
            axios({
                url: url,
                method: method,
                headers: {
                    'Content-Type': 'application/json;charset=UTF-8',
                    Authorization: localStorage.getItem('token')
                },
                data: JSON.stringify(data)
            })
                .then(function (res) {
                    let data = res.data
                    if (data.code === 0) {
                        resolve(data)
                    } else if (data.code === -1) {
                        ElNotification({
                            title: '提示',
                            message: data.message,
                            type: 'error'
                        })
                    } else {
                        ElNotification({
                            title: '提示',
                            message: data.message,
                            type: 'warning'
                        })
                    }
                })
                .catch(function (error) {
                    if (error.response) {
                        generateErrorOperation(error.response.status)
                    } else if (error.request) {
                    } else {
                        ElNotification({
                            title: '提示',
                            message: error.message,
                            type: 'error'
                        })
                    }
                })
        })
    },

    fetchForm: function (options) {
        let url = options.url
        let method = options.method
        let data = options.data
        if (url == undefined || url == null || url == '') {
            throw '缺少url参数'
            return
        }

        if (method == undefined || method == null || method == '') {
            method = 'get'
        }

        if (data == undefined || data == null || data == '') {
            throw '缺少data参数'
            return
        }
        return new Promise((resolve, reject) => {
            axios({
                url: url,
                method: method,
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                    Authorization: localStorage.getItem('token')
                },
                params: data
            })
                .then(function (res) {
                    let data = res.data
                    if (data.code === 0) {
                        resolve(data)
                    } else {
                        ElNotification({
                            title: '提示',
                            message: data.message,
                            type: 'warning'
                        })
                    }
                })
                .catch(function (error) {
                    if (error.response) {
                        generateErrorOperation(error.response.status)
                    } else if (error.request) {
                    } else {
                        ElNotification({
                            title: '提示',
                            message: error.message,
                            type: 'error'
                        })
                    }
                })
        })
    },

    fetchFile: function (options) {
        let url = options.url
        let method = options.method
        let data = options.data
        if (url == undefined || url == null || url == '') {
            throw '缺少url参数'
            return
        }

        if (method == undefined || method == null || method == '') {
            method = 'post'
        }

        if (data == undefined || data == null || data == '') {
            throw '缺少data参数'
            return
        }

        return new Promise((resolve, reject) => {
            axios({
                url: url,
                method: method,
                headers: {
                    'Content-Type': 'multipart/form-data',
                    Authorization: localStorage.getItem('token')
                },
                timeout: 60000,
                data: data
            })
                .then(function (res) {
                    let data = res.data
                    if (data.code === 0) {
                        resolve(data)
                    } else {
                        ElNotification({
                            title: '提示',
                            message: data.message,
                            type: 'warning'
                        })
                    }
                })
                .catch(function (error) {
                    if (error.response) {
                        generateErrorOperation(error.response.status)
                    } else if (error.request) {
                    } else {
                        ElNotification({
                            title: '提示',
                            message: error.message,
                            type: 'error'
                        })
                    }
                })
        })
    }
}

function generateErrorOperation(code) {
    let message = ''
    switch (code) {
        case 401:
            localStorage.clear()
            router.push('/login')
            break
        case 403:
            message = '未授权的操作'
            break
        case 404:
            message = '您所请求的资源无法找到'
            break
        case 500:
            message = '服务器内部错误，无法完成请求'
            break
        default:
            message = '服务暂不可用，请稍后再试'
    }
    if (message != '') {
        ElNotification({
            title: '提示',
            message: message,
            type: 'error'
        })
    }
}
